package cn.lliiooll.launcher.thread;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import cn.lliiooll.launcher.log.LogOut;
import cn.lliiooll.launcher.update.Update;

public class GetPmToServer implements Runnable{
	
	public GetPmToServer() {
		Thread t = new Thread(this, "GetPmToServer");
		t.run();
	}

	@SuppressWarnings("resource")
	@Override
	public void run() {
		try {
		Socket c = new Socket(Update.link,Update.port);
		DataInputStream ifs = new DataInputStream(c.getInputStream()); 
		OutputStream outs = c.getOutputStream();
		DataOutputStream out = new DataOutputStream(outs);
		File filepath1 = new File("lib");
		File file = new File(filepath1 + "\\publicmsg.data");
		if(!(filepath1.exists())) {
			filepath1.mkdir();
			if(!(file.exists())) {
				file.createNewFile();
				//InputStream in = new DataInputStream(ifs);
				FileOutputStream fos = new FileOutputStream(file);
						LogOut.info("正在下载");
						byte[] b = new byte[1024];
						int i = 0;
						out.writeInt(2);
						while((i = ifs.read(b,0,b.length)) != -1) {
							fos.write(b, 0, i);
						}
						LogOut.info("下载完成");
						fos.flush();
						ifs.close();
						c.close();
			}else {
				FileOutputStream fos = new FileOutputStream(file);
				LogOut.info("正在下载");
				byte[] b = new byte[1024];
				int i = 0;
				out.writeInt(2);
				while((i = ifs.read(b,0,b.length)) != -1) {
					fos.write(b, 0, i);
				}
				LogOut.info("下载完成");
				fos.flush();
				ifs.close();
				c.close();
			}
		}else {
				//InputStream in = new DataInputStream(ifs);
			if(!(file.exists())) {
				file.createNewFile();
				//InputStream in = new DataInputStream(ifs);
				FileOutputStream fos = new FileOutputStream(file);
						LogOut.info("正在下载");
						byte[] b = new byte[1024];
						int i = 0;
						out.writeInt(2);
						while((i = ifs.read(b,0,b.length)) != -1) {
							fos.write(b, 0, i);
						}
						LogOut.info("下载完成");
						fos.flush();
						ifs.close();
						c.close();
			}else {
				FileOutputStream fos = new FileOutputStream(file);
				LogOut.info("正在下载");
				byte[] b = new byte[1024];
				int i = 0;
				out.writeInt(2);
				while((i = ifs.read(b,0,b.length)) != -1) {
					fos.write(b, 0, i);
				}
				LogOut.info("下载完成");
				fos.flush();
				ifs.close();
				c.close();
			}
		}
		}catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		
	}

}
